package com.spider.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.spider.constant.WangHuaUrl;
import com.spider.entity.WanghuaEntity;
import com.spider.mapper.HeownsWanghuaMapper;
import com.spider.util.StringUtil;
import com.spider.util.WanghuaHttpUtil;
import com.spider.util.WanghuaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * 网化科技
 */
@Slf4j
@Service
public class WangHuaServiceImpl implements WangHuaService {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    HeownsWanghuaMapper mapper;

    /**
     * 获取token
     */
    @Override
    public String getToken(){
        //拼接参数
        String url= WangHuaUrl.PRODUCT_URL+WangHuaUrl.TOKEN_URL;
        JSONObject jb=new JSONObject(new LinkedHashMap());
        jb.put("app_id",WangHuaUrl.TEST_APPID);
        jb.put("app_secret",WangHuaUrl.TEST_APP_SECRET);
        System.out.println(jb);
        ResponseEntity<String> result=restTemplate.postForEntity(url,jb,String.class);
        System.out.println(result.toString());
        log.info(result.toString());
        if(result.getStatusCodeValue()==200){
            JSONObject jbBody=JSONObject.parseObject(result.getBody());
            JSONObject jbData= (JSONObject) jbBody.get("data");
            String token= (String) jbData.get("token");
            System.out.println(token);
            log.info(token);
            return token;
        }
        return null;
    }

    /**
     * 库存新增
     * @param num 不为空则只推送一条
     */
    @Override
    public String stockInsert(String num) throws Exception {
        String result="";
        //拼接参数
        String url= WangHuaUrl.PRODUCT_URL+WangHuaUrl.SYS_URL;
        String token = this.getToken();
        System.out.println(token);
        //拼接body
        List<WanghuaEntity> list = mapper.selectInsert();
        //List<WanghuaEntity> list =WanghuaUtil.getInsertListTest();
        if(list.isEmpty()){
            log.info("网化科技新增产品推送无数据，推送结束");
        }else{
            log.info("网化科技新增产品推送查询结果集数量为"+list.size());
            try{
                JSONObject body= WanghuaUtil.convertProcudtBody(list);
                log.info("转化结果"+body);
                if(body==null){
                    log.info("实体拼接数据失败缺乏数据，跳过此数据");
                }
                log.info(body.toString());
                String res= WanghuaHttpUtil.postRequest(token,url,body);
                log.info("调用实际接口成功");
                result=res;
                log.info(res);
                JSONObject jb= JSON.parseObject(res);
                String code= (Integer) jb.get("code")+"";
                if(StringUtil.isNotEmpty(code)&&code.equals("0")){
                    log.info("插入数据库数量"+list.size());
                    //返回结果成功，插入本地库
                    for(WanghuaEntity entity:list){
                        entity.setStatus("成功");
                        entity.setCountStr(entity.getCount()+"");
                        mapper.saveWanghuaInsert(entity);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                log.error("网化科技新增产品推送失败，失败原因："+e.getMessage());
            }
        }
        return result;
    }

    /**
     * 修改价格
     * @param num 不为空则只推送一条
     */
    @Override
    public String updatePrice(String num){
        String result="";
        //拼接参数
        String url= WangHuaUrl.PRODUCT_URL+WangHuaUrl.SYS_URL;
        String token = this.getToken();
        System.out.println(token);
        //拼接body
        List<WanghuaEntity> list = mapper.selectUpdatePrice();
        if(list.isEmpty()){
            log.info("网化科技修改价格推送无数据，推送结束");
        }else{
            log.info("网化科技修改价格推送查询结果集数量为"+list.size());
            try{
                JSONObject body= WanghuaUtil.convertProcudtBody(list);
                if(body==null){
                    log.info("实体拼接数据失败缺乏数据，跳过此数据");
                }
                System.out.println(body.toString());
                String res= WanghuaHttpUtil.postRequest(token,url,body);
                result=res;
                System.out.println(res);
                JSONObject jb= JSON.parseObject(res);
                String code= (Integer) jb.get("code")+"";
                if(StringUtil.isNotEmpty(code)&&code.equals("0")){
                    //返回结果成功，插入本地库
                    for(WanghuaEntity entity:list){
                        entity.setStatus("成功");
                        entity.setCountStr(entity.getCount()+"");
                        mapper.updatePrice(entity);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                log.error("网化科技修改价格推送失败，失败原因："+e.getMessage());
            }
        }
        return result;
    }

    /**
     * 修改库存
     * @param num 不为空则只推送一条
     */
    @Override
    public String updateStock(String num){
        String result="";
        //拼接参数
        String url= WangHuaUrl.PRODUCT_URL+WangHuaUrl.SYS_URL;
        String token = this.getToken();
        System.out.println(token);
        //拼接body
        List<WanghuaEntity> list = mapper.selectJiliStock();
        if(list.isEmpty()){
            log.info("网化科技修改库存推送无数据，推送结束");
        }else{
            log.info("网化科技修改库存推送查询结果集数量为"+list.size());
            try{
                JSONObject body= WanghuaUtil.convertProcudtBody(list);
                if(body==null){
                    log.info("实体拼接数据失败缺乏数据，跳过此数据");
                }
                System.out.println(body.toString());
                String res= WanghuaHttpUtil.postRequest(token,url,body);
                result=res;
                System.out.println(res);
                JSONObject jb= JSON.parseObject(res);
                String code= (Integer) jb.get("code")+"";
                if(StringUtil.isNotEmpty(code)&&code.equals("0")){
                    //返回结果成功，插入本地库
                    for(WanghuaEntity entity:list){
                        entity.setStatus("成功");
                        entity.setCountStr(entity.getCount()+"");
                        mapper.updateStock(entity);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                log.error("网化科技修改库存推送失败，失败原因："+e.getMessage());
            }
        }
        return result;
    }
    /**
     * 下架产品
     * @param num 不为空则只推送一条
     */
    @Override
    public String deleteProduct(String num){
        String result="";
        //拼接参数
        String url= WangHuaUrl.PRODUCT_URL+WangHuaUrl.SYS_URL;
        String token = this.getToken();
        System.out.println(token);
        //拼接body
        List<WanghuaEntity> list = mapper.selectOffShelf();
        if(list.isEmpty()){
            log.info("网化科技下架产品推送无数据，推送结束");
        }else{
            log.info("网化科技下架产品推送查询结果集数量为"+list.size());
            try{
                JSONObject body= WanghuaUtil.convertProcudtBody(list);
                if(body==null){
                    log.info("实体拼接数据失败缺乏数据，跳过此数据");
                }
                System.out.println(body.toString());
                String res= WanghuaHttpUtil.postRequest(token,url,body);
                result=res;
                System.out.println(res);
                JSONObject jb= JSON.parseObject(res);
                String code= (Integer) jb.get("code")+"";
                if(StringUtil.isNotEmpty(code)&&code.equals("0")){
                    //返回结果成功，插入本地库
                    for(WanghuaEntity entity:list){
                        entity.setStatus("成功");
                        entity.setCountStr(entity.getCount()+"");
                        mapper.deleteProduct(entity);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                log.error("网化科技下架产品推送失败，失败原因："+e.getMessage());
            }
        }
        return result;
    }
}
